|
|
|
|
|
|
|
Figure 6.3.
Passing variables by
reference can cause
the original data to be
changed
unintentionally. |
|
|
|
|
|
|
|
|
Figure 6.4.
Unless the data must
be changed in the
routine, using
ByVal is the accepted
method of passing
arguments. |
|
|
|
|
|
|
|
|
To sum this all up, when you create routineseither subroutines or functionsyou must decide how the data is going to be processed. Remember that declaring all of your variables as public will make it easier for you to write the initial code, but when you must test and fix problems, public variables will give you a very bad headache. Finally, when passing arguments, remember that if you do not explicitly set the way the arguments are passed, they will be passed by reference. If that is the case, do not modify the variables that were passed while in the routine. If you must return a new value, you should consider using a function. In addition, you can now define arguments for a Sub procedure or Function as Optional. This allows you to pass data in that parameter only when needed. |
|
|
|
|
|
|
|
|
Using functions and subroutines is but one way to affect how your application code will be processed. When designing complex database applications, many sections of code will perform the same process, just on different data. As an example, your application could accept dates as one of its inputs. Besides editing the data to see whether it is a valid date, you might have to check to see whether the date is allowed. However, you might be performing this edit check on several different data input fields at different times in the application. By calling a function, passing the date variable, and possibly the range against which to check it, would allow you to check the return value for any errors. |
|
|
|
|
|
|
|
|
If the function could return more than just a True or False condition, you would need to write code that would check for each of the possible return values that might be returned. The process of checking the return value of a function or comparing any two variables or values in your application is what makes the application perform. Without the capability |
|
|
|
|
|